package at.fhooe.swa.client.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.swing.ImageIcon;

public class Resources {

	private static Properties iconsProperties;
	private static String iconDir; // stored to save access time for every icon
	public static String icon_16x16 = "16x16";
	
	private static final String SERVICE_IP_KEY = "server.ip";
	private static final String SERVICE_PORT_KEY = "server.port";
	
	private static Properties applicationProperties;
	private static Properties sensorProperties;
	
	/**
	 * Returns the lazy initialised editor settings properties.
	 * These settings can not be changed by the user.
	 * For the changeable settings see: {@link #getChangeableEditorProperties()}
	 * 
	 * @return properties with unchangeable editor settings
	 */
	private static Properties getApplicationProperties() {
		if(applicationProperties == null) {
			applicationProperties = new Properties();
			try {
				// load the settings that are unchangeable
				InputStream s = applicationProperties.getClass().getResourceAsStream("/application.properties");
				applicationProperties.load(s);
				s.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return applicationProperties;
	}
	
	/**
	 * Returns the lazy initialised editor settings properties.
	 * These settings can not be changed by the user.
	 * For the changeable settings see: {@link #getChangeableEditorProperties()}
	 * 
	 * @return properties with unchangeable editor settings
	 */
	private static Properties getSensorProperties() {
		if(sensorProperties == null) {
			sensorProperties = new Properties();
			try {
				// load the settings that are unchangeable
				InputStream s = sensorProperties.getClass().getResourceAsStream("/sensor.properties");
				sensorProperties.load(s);
				s.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sensorProperties;
	}
	
	/**
	 * Returns the lazy initialized icon properties.
	 * 
	 * @return properties with icon paths
	 */
	private static Properties getIconProperties() {
		if(iconsProperties == null) {
			 iconsProperties = new Properties();
			 try {
				 InputStream s = iconsProperties.getClass().getResourceAsStream("/icons.properties");
				iconsProperties.load(s);
				s.close();
				iconDir = iconsProperties.getProperty("icondir");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return iconsProperties;
	}
	
	/**
	 * Returns the small icon with the given key.
	 * 
	 * @param key key to gain the wanted small icon
	 * @return small icon or null if not found
	 */
	public static ImageIcon getIcon(String key, String dimension) {
		try {
			if (dimension != null && !dimension.equals(""))
				return new ImageIcon(getIconProperties().getClass().getResource(iconDir.concat(getIconProperties().getProperty(key + "." + dimension))));
			else
				return new ImageIcon(getIconProperties().getClass().getResource(iconDir.concat(getIconProperties().getProperty(key))));
		} catch(Exception e) {
			return null;
		}
	}
	
	public static String getSensorProperty(String key)
	{
		try
		{
			return getSensorProperties().getProperty(key);
		}
		catch (Exception e)
		{
			return null;
		}
	}
	
	public static String getLabelString(String key)
	{
		return getApplicationProperties().getProperty(key);
	}
	
	public static String getServiceIp()
	{
		return getApplicationProperties().getProperty(SERVICE_IP_KEY);
	}
	
	public static int getServicePort()
	{
		return Integer.parseInt(getApplicationProperties().getProperty(SERVICE_PORT_KEY));
	}
	
}
